var isAdd = true;
var vm = new Vue({
    el:"#main",
    data:{
        tabType:0,
        search:{
            current:1,
            size:10,
            key:''
        },
        total:0,
        listLoading:true,
        tableHeight:"",
        tableData:[],
        clusters:[],
        addVisible:false,
        codeDisabled:false,
        form:{
            id:"",
            plantCluster:"",
            plantCode:"",
            englishName:"",
            chineseName:""
        },
        dialogTitle:"",
        tab2:{
            isFirst:true,
            listLoading:false,
            search:{
                current: 1,
                size: 10,
                plant:"",
                user:"",
                status:0,
            },
            statusArr:[
                {"value":0,"label":'All'},
                {"value":1,"label":'Yes'},
                {"value":2,"label":'No'}
            ],
            total:0,
            tableData:[],
            addVisible:false,
            form:{
                id:"",
                plantCode:"",
                procurementManager:"",
                procurementUser:""
            },
            plants:[],
            ppmDisabled:false,
            codeDisabled:false,
            ppms:[],
            pps:[]
        }
    },
    methods:{
        changeTab:function (tab) {
            this.tabType=tab.index;
            if(this.tabType==1){
                this.getUserData();
                this.getAllPlant();
                if(this.tab2.isFirst){
                    this.tab2.isFirst=false;
                    this.getPPM();
                    this.getPP();
                }
            }
        },
        exportPlant:function(){
            window.location.href ="/data/plant/export?"+jsonUtil.serialize(this.search)
        },
        getData:function () {
            this.listLoading = true;
            VanyAjax("/data/plant/list", this.search, "POST").done(function (data) {
                this.total = data.total;
                this.tableData=data.records;
                this.listLoading = false;
            }, false, this);
        },
        getClusters:function(){
            VanyAjax("/sys/dictionary/query?code=cluster",{}, "GET").done(function (data) {
                this.clusters=data;
            }, false, this);
        },
        submitForm:function () {
            var url = isAdd?"/data/plant/save":"/data/plant/update";
            VanyAjax(url,this.form, "POST").done(function (data) {
               this.addVisible=false;
               this.getData();
            }, true, this);
        },
        addPlant:function(){
            this.dialogTitle=$("#addTitle").val();
            this.codeDisabled = false;
            isAdd = true;
            this.form.id=""
            this.form.plantCluster="";
            this.form.plantCode="";
            this.form.englishName="";
            this.form.chineseName="";
            this.addVisible=true;
        },
        editPlant:function (row) {
            this.dialogTitle=$("#editTitle").val();
            this.codeDisabled = true;
            isAdd = false;
            this.form.id=row.id;
            this.form.plantCluster=row.plantCluster;
            this.form.plantCode=row.plantCode;
            this.form.englishName=row.englishName;
            this.form.chineseName=row.chineseName;
            this.addVisible=true;
        },
        deletePlant:function (row) {
            this.$confirm('Confirm deletion?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                VanyAjax("/data/plant/delete",{"id":row.id}, "POST").done(function (data) {
                    vm.getData();
                }, true, this);
            }).catch(function() {})
        },
        getUserData:function () {
            this.tab2.listLoading = true;
            VanyAjax("/data/plantUser/list", this.tab2.search, "POST").done(function (data) {
                this.tab2.total = data.total;
                this.tab2.tableData=data.records;
                this.tab2.listLoading = false;
            }, false, this);
        },
        addPlantUser:function(){
            this.dialogTitle=$("#addTitle2").val();
            isAdd = true;
            this.tab2.ppmDisabled=false;
            this.tab2.codeDisabled=false;
            this.tab2.form.id="";
            this.tab2.form.plantCode="";
            this.tab2.form.procurementManager="";
            this.tab2.form.procurementUser="";
            this.tab2.addVisible=true;
        },
        editPlantUser:function (row) {
            this.dialogTitle=$("#editTitle2").val();
            isAdd = false;
            this.tab2.ppmDisabled=false;
            this.tab2.codeDisabled=true;
            this.tab2.form.id=row.id;
            this.tab2.form.plantCode=row.plantCode;
            this.tab2.form.procurementManager=row.procurementManager;
            this.tab2.form.procurementUser=row.procurementUser;
            this.tab2.addVisible=true;
        },
        deletePlantUser:function (row) {
            if(row.id==null){
                return;
            }
            this.$confirm('Confirm deletion?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                VanyAjax("/data/plantUser/delete",{"id":row.id}, "POST").done(function (data) {
                    console.log("delete success");
                    vm.getUserData();
                }, true, this);
            }).catch(function() {})
        },
        submitUserForm:function () {
            var url = isAdd?"/data/plantUser/save":"/data/plantUser/update";
            VanyAjax(url,this.tab2.form, "POST").done(function (data) {
                this.tab2.addVisible=false;
                this.getUserData();
            }, true, this);
        },
        getAllPlant:function () {
            VanyAjax("/data/plant/all",{}, "GET").done(function (data) {
                this.tab2.plants=data;
            }, false, this);
        },
        changeCode:function () {
            //编辑模式不用处理
            if(!isAdd)return;
            VanyAjax("/data/plantUser/one",{"code":this.tab2.form.plantCode}, "POST").done(function (data) {
                console.log(data);
                if(data!=null){
                    this.tab2.form.procurementManager=data.procurementManager;
                    this.tab2.ppmDisabled=true;
                }else{
                    this.tab2.ppmDisabled=false;
                }
            }, false, this);
        },
        getPPM:function () {
            VanyAjax("/sys/user/selectByRoleIds?roleIds=PPM",{}, "GET").done(function (data) {
                this.tab2.ppms=data;
            }, false, this);
        },
        getPP:function () {
            VanyAjax("/sys/user/selectByRoleIds?roleIds=PP",{}, "GET").done(function (data) {
                this.tab2.pps=data;
            }, false, this);
        }
    },
    mounted:function () {
        this.tableHeight = window.innerHeight - 450;
        this.getData();
        this.getClusters();
    }
})
